package com.ylsk.check.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Lists;
import com.ylsk.check.domain.dto.DeptConsUserDTO;
import com.ylsk.check.domain.so.DeptConsUserSO;
import com.ylsk.check.domain.vo.DeptConsUserVO;
import com.ylsk.common.config.SnowIdConfig;
import com.ylsk.common.core.domain.entity.SysUser;
import com.ylsk.common.utils.SecurityUtils;
import com.ylsk.common.utils.StringUtils;
import com.ylsk.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ylsk.common.annotation.Log;
import com.ylsk.common.core.controller.BaseController;
import com.ylsk.common.core.domain.AjaxResult;
import com.ylsk.common.enums.BusinessType;
import com.ylsk.check.domain.DeptConsUser;
import com.ylsk.check.service.IDeptConsUserService;
import com.ylsk.common.utils.poi.ExcelUtil;
import com.ylsk.common.core.page.TableDataInfo;

/**
 * 大队施工人员Controller
 *
 * @author yangsy
 * @date 2024-01-29
 */
@RestController
@RequestMapping("/check/deptConsUser")
public class DeptConsUserController extends BaseController
{
    @Autowired
    private IDeptConsUserService deptConsUserService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private SnowIdConfig snowIdConfig;

    /**
     * 查询大队施工人员列表
     */
    @PreAuthorize("@ss.hasPermi('check:deptConsUser:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeptConsUserSO deptConsUserSO)
    {
        startPage();
        List<DeptConsUserVO> list = deptConsUserService.selectDeptConsUserList(deptConsUserSO);
        return getDataTable(list);
    }

    /**
     * 导出大队施工人员列表
     */
    @PreAuthorize("@ss.hasPermi('check:deptConsUser:export')")
    @Log(title = "大队施工人员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeptConsUserSO deptConsUserSO)
    {
        List<DeptConsUserVO> list = deptConsUserService.selectDeptConsUserList(deptConsUserSO);
        ExcelUtil<DeptConsUserVO> util = new ExcelUtil<DeptConsUserVO>(DeptConsUserVO.class);
        util.exportExcel(response, list, "大队施工人员数据");
    }

    /**
     * 获取大队施工人员详细信息
     */
    @PreAuthorize("@ss.hasPermi('check:deptConsUser:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("userId") Long userId)
    {
        return success(deptConsUserService.selectDeptConsUserByDeptConsUserId(userId));
    }

    /**
     * 新增大队施工人员
     */
    @PreAuthorize("@ss.hasPermi('check:deptConsUser:add')")
    @Log(title = "大队施工人员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeptConsUserDTO deptConsUserDTO) {
        SysUser user = new SysUser();
        BeanUtils.copyProperties(deptConsUserDTO,user);
        if (!userService.checkUserNameUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("新增用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        int result = deptConsUserService.insertDeptConsUser(user,deptConsUserDTO);
        return success(result);
    }

    /**
     * 修改大队施工人员
     */
    @PreAuthorize("@ss.hasPermi('check:deptConsUser:edit')")
    @Log(title = "大队施工人员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeptConsUserDTO deptConsUserDTO)
    {
        SysUser user = new SysUser();
        BeanUtils.copyProperties(deptConsUserDTO,user);
        if (!userService.checkUserNameUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败，手机号码已存在");
        }
        else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
        {
            return error("修改用户'" + user.getUserName() + "'失败，邮箱账号已存在");
        }
        return toAjax(deptConsUserService.updateDeptConsUser(user,deptConsUserDTO));
    }

    /**
     * 删除大队施工人员
     */
    @PreAuthorize("@ss.hasPermi('check:deptConsUser:remove')")
    @Log(title = "大队施工人员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deptConsUserIds}")
    public AjaxResult remove(@PathVariable Long[] deptConsUserIds)
    {
        return toAjax(deptConsUserService.deleteDeptConsUserByDeptConsUserIds(deptConsUserIds));
    }
}
